/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.contract.app;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.contract.entity.ContractSymbol;
import com.pig4cloud.pigx.contract.entity.RippleContractSymbolHierarchy;
import com.pig4cloud.pigx.contract.mapper.RippleContractSymbolHierarchyMapper;
import com.pig4cloud.pigx.contract.service.ContractSymbolService;
import com.pig4cloud.pigx.contract.utils.ContractUtils;
import com.pig4cloud.pigx.contract.vo.AppContractSymbolDetailVO;
import com.pig4cloud.pigx.contract.vo.ContractSymbolOptionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * app端币对Controller
 *
 * @since 2022-08-09 20:29:23
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/app/symbol")
@Api(value = "appSymbol", tags = "app币对管理")
public class AppContractSymbolController {

	private final ContractSymbolService contractSymbolService;

	private final RippleContractSymbolHierarchyMapper hierarchyMapper;

	private final ContractUtils utils;

	/**
	 * 根据id查询详情
	 *
	 * @param id 币对id
	 * @return {@link R< AppContractSymbolDetailVO>}
	 * @since 2022/8/24 22:21
	 */
	@ApiOperation(value = "根据id查询详情", notes = "根据id查询详情")
	@GetMapping("/detail/{id}")
//	@PreAuthorize("@pms.hasPermission('contract_contractsymbol_view')")
	public R<JSONObject> detail(@PathVariable("id") Long id) {
		AppContractSymbolDetailVO res = contractSymbolService.appDetail(id);
		List<RippleContractSymbolHierarchy> hierarchies = hierarchyMapper.selectList(
				new QueryWrapper<RippleContractSymbolHierarchy>() {{
					eq("symbol_id", id);
					select("start_multiple,end_multiple,min_position,max_position");
				}});
		JSONObject object = new JSONObject();
		utils.setScaleBySymbol(res,res.getName());
		object.put("detailed", res);
		object.put("hierarchy", hierarchies);
		return R.ok(object);
	}

	@ApiOperation(value = "获取所有币对", notes = "获取所有币对")
	@GetMapping("/all")
	public R getSymbolAllList() {
		BaseMapper<ContractSymbol> mapper = contractSymbolService.getBaseMapper();
		QueryWrapper<ContractSymbol> wrapper = new QueryWrapper<>();
		wrapper.eq("is_deleted", "0");
		wrapper.eq("status", "1");
		List<ContractSymbol> symbols = mapper.selectList(wrapper);
		List<ContractSymbolOptionVO> list = new ArrayList<>();
		symbols.forEach(e -> {
			ContractSymbolOptionVO vo = new ContractSymbolOptionVO();
			BeanUtils.copyProperties(e, vo);
			list.add(vo);
		});
		return R.ok(list);
	}

	@ApiOperation(value = "根据id查询app币对详情", notes = "根据id查询app币对详情")
	@GetMapping("/{id}")
	public R getDetailed(@PathVariable("id") Long id) {
		return R.ok(contractSymbolService.getDetailed(id));
	}
}
